package com.cn.xiaonuo.slbfinances.slpabank.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.attractinfo.entity.SlAttractUser;
import com.cn.xiaonuo.slbfinances.attractinfo.enums.SlAttractUserExceptionEnum;
import com.cn.xiaonuo.slbfinances.attractinfo.param.SlAttractUserParam;
import com.cn.xiaonuo.slbfinances.slpabank.entity.SlPaMember;
import com.cn.xiaonuo.slbfinances.slpabank.mapper.SlPaMemberMapper;
import com.cn.xiaonuo.slbfinances.slpabank.service.IPaMemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PaMemberServiceImpl extends ServiceImpl<SlPaMemberMapper, SlPaMember> implements IPaMemberService {
    @Resource
    public SlPaMemberMapper slPAMemberMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryPaMemberHT(Integer pageNo, Integer pageSize, SlPaMember slPaMember) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        if (null == pageNo || pageNo <= 1) {
            pageNo = 1;
        }
        if (null == pageSize || Integer.valueOf(pageSize) <= 1) {
            pageSize = 10;
        }
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        LambdaQueryWrapper<SlPaMember> queryWrapper = new LambdaQueryWrapper<>();
        if (slPaMember.getSlUserId() != null && slPaMember.getSlUserId() != 0) {
            queryWrapper.eq(SlPaMember::getSlUserId, slPaMember.getSlUserId());
        }
        if (slPaMember.getStatus() != null && slPaMember.getStatus() != "") {
            queryWrapper.eq(SlPaMember::getStatus, slPaMember.getStatus());
        }
        List<SlPaMember> lists = slPAMemberMapper.selectList(queryWrapper);
        PageInfo<SlPaMember> pageInfo = new PageInfo(lists);
        if (null != lists && lists.size() >= 0) {
            message.setCode("200");
            message.setSuccess(true);
            message.setData(pageInfo);
            message.setMessage("查询平安用户数据成功");
            return message;
        }
        message.setCode("-1");
        message.setSuccess(false);
        message.setData(pageInfo);
        message.setMessage("查询平安用户数据失败");
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage unbindRelateAcct(Integer slUserId) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String format = formatter.format(date);
            LambdaQueryWrapper<SlPaMember> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SlPaMember::getSlUserId, slUserId);
            SlPaMember slPaMember = slPAMemberMapper.selectOne(queryWrapper);
            if (null == slPaMember) {
                message.setMessage("没有查询到该账号");
                message.setSuccess(false);
                message.setCode("-1");
                return message;
            }
            slPaMember.setStatus("0");
            slPaMember.setUpdateTime(format);
            slPAMemberMapper.updateById(slPaMember);
            message.setCode("200");
            message.setSuccess(true);
            message.setMessage("操作成功");
        }catch (Exception e){
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("操作失败");
            e.printStackTrace();
        }
        return message;
    }

    /**
     * 获取会员信息
     *
     * @author 卢阳阳
     * @date 2022-12-23 14:55:13
     */
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public SlPaMember getMemberById(Integer id) {
        SlPaMember slPaMember = this.getById(id);
        return slPaMember;
    }

}
